package com.faneui.transfer.gateway.controller;

import java.util.UUID;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import com.faneui.transfer.common.code.AccountTypeEnum;
import com.faneui.transfer.common.code.OrderStatusEnum;
import com.faneui.transfer.dal.model.Cust;
import com.faneui.transfer.dal.model.Order;
import com.faneui.transfer.service.AccountService;
import com.faneui.transfer.service.CustService;
import com.faneui.transfer.service.OrderService;

import com.faneui.transfer.gateway.vo.req.AddOrderReq;
import com.faneui.transfer.gateway.vo.req.BindingReq;
import com.faneui.transfer.gateway.vo.req.LoginReq;
import com.faneui.transfer.gateway.vo.req.RegisterReq;
import com.faneui.transfer.gateway.vo.resp.AddOrderResp;
import com.faneui.transfer.gateway.vo.resp.BaseResp;
import com.faneui.transfer.gateway.vo.resp.LoginResp;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

@RestController
@Api("转")
@RequestMapping(value = "transfer/")
public class TransferController extends AbstractController{

	@Autowired
	private CustService custService;

	@Autowired
	private AccountService accountService;

	@Autowired
	private OrderService orderService;
 
	@ApiOperation(value = "注册")
	@RequestMapping(value = "register", method = RequestMethod.GET)
	@ResponseBody
	public BaseResp register(RegisterReq req) throws Exception {
		Cust cust = new Cust();
		cust.setLoginName(req.getLoginName());
		cust.setLoginPwd(req.getLoginPwd());
		custService.insertCust(cust);
		return new BaseResp();
	}

	@ApiOperation(value = "登录")
	@RequestMapping(value = "login", method = RequestMethod.GET)
	@ResponseBody
	public LoginResp login(LoginReq req) throws Exception{
		LoginResp resp = new LoginResp();
		Cust cust = custService.login(req.getLoginName(), req.getLoginPwd());
		String uuid = UUID.randomUUID().toString(); 
		this.keepLogin(uuid, cust);
		resp.setUuid(uuid);
		return resp;
	}

	@ApiOperation(value = "账号绑定")
	@RequestMapping(value = "binding", method = RequestMethod.GET)
	@ResponseBody
	public BaseResp binding(BindingReq req) throws Exception{
		String ticket = req.getUuid() ;
		Cust cust = this.getLoginCust(ticket) ;
		accountService.bindingAccount(cust.getId(), req.getTypeEnum(), req.getAccountNo() );
		return new BaseResp();
	}

	@ApiOperation(value = "生成订单")
	@RequestMapping(value = "addOrder", method = RequestMethod.GET)
	@ResponseBody
	public AddOrderResp addOrder(@ModelAttribute AddOrderReq req) throws Exception{
		AddOrderResp resp = new AddOrderResp();
		Cust cust = this.getLoginCust(req.getUuid()) ;
		Long fromAccountId = accountService.queryAccount(cust.getId(), AccountTypeEnum.weixing).getId();
		Long toAccountId = accountService.queryAccount(cust.getId(), AccountTypeEnum.zhifubao).getId() ;
		Order order = new Order();
		order.setCustId(cust.getId());
		order.setFromAccountId(fromAccountId);
		order.setToAccountId(toAccountId);
		order.setStatus(OrderStatusEnum.pay.getCode());
		orderService.addOrder(order); 
		resp.setOrderId(order.getId());
		return resp;
	}
	 

}
